package org.erp.spu.controller;

import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.bo.SpuSeriesBo;
import org.erp.spu.entity.dto.SpuSeriesDto;
import org.erp.spu.entity.vo.SpuSeriesVo;
import org.erp.spu.service.SpuSeriesService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 系列表(SpuSeries)表控制层
 *
 * @since 2025-11-05 17:45:57
 */
@RequestMapping("series")
@RestController
public class SpuSeriesController {

    @Resource
    private SpuSeriesService spuSeriesService;

    /**
     * 分页查询所有数据
     *
     * @param spuSeriesBo 查询实体
     * @return 所有数据
     */
    @GetMapping("getListByPage")
    public Result<DataPageResp<SpuSeriesVo>> selectAll(SpuSeriesBo spuSeriesBo) {
        SpuSeriesDto spuSeriesDto = new SpuSeriesDto();
        BeanUtils.copyProperties(spuSeriesBo, spuSeriesDto);
        PageQueryModel<SpuSeriesVo> queryModel = spuSeriesService.getListByPage(spuSeriesDto);
        return success(DataPageResp.of(queryModel.getTotal(), queryModel.getDataList()));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<SpuSeriesVo> selectOne(@PathVariable Long id) {
        return Result.success(spuSeriesService.getSpuSeriesVoById(id));
    }

    /**
     * 获取所有数据
     *
     * @param spuSeriesBo 筛选条件
     * @return 所有数据
     */
    @GetMapping("getList")
    public Result<List<SpuSeriesVo>> getList(SpuSeriesBo spuSeriesBo) {
        return Result.success(spuSeriesService.getList(spuSeriesBo));
    }

    /**
     * 新增数据
     *
     * @param spuSeriesDto 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result<Boolean> insert(@RequestBody SpuSeriesDto spuSeriesDto) {
        boolean result = spuSeriesService.addSpuSeries(spuSeriesDto);
        return result ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改数据
     *
     * @param spuSeriesDto 实体对象
     * @return 修改结果
     */
    @PostMapping("/upd")
    public Result<Boolean> update(@RequestBody SpuSeriesDto spuSeriesDto) {
        boolean result = spuSeriesService.updateSpuSeries(spuSeriesDto);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @PostMapping("del")
    public Result<Boolean> delete(@RequestBody SpuSeriesDto spuSeriesDto) {
        boolean result = spuSeriesService.removeById(spuSeriesDto.getId());
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }

}
